package com.example.demo;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jgdt.tools.decrypt.annotation.Decrypt;
import com.jgdt.tools.decrypt.annotation.Encrypt;
import com.jgdt.tools.decrypt.config.EncryptProperties;
import com.jgdt.tools.decrypt.utils.AESUtil;
import jdk.nashorn.internal.ir.annotations.Immutable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * <p>
 *
 * </p>
 *
 * @author chang.zhou
 * @version 1$
 * @date Created in 2021/5/13 16:15
 */
@RestController
public class DemoController {

    Logger logger = LoggerFactory.getLogger(DemoController.class);

    private ObjectMapper objectMapper = new ObjectMapper();
    @Autowired
    private EncryptProperties encryptProperties;

    @GetMapping("/encrypt")
    @Encrypt
    public RestBody test() throws Exception {
        RestBody<String> restBody = new RestBody<>(1, "这是一条message", "测试data");
        String str = this.objectMapper.writeValueAsString(restBody);
        logger.info(AESUtil.encrypt(str.getBytes(), encryptProperties.getKey().getBytes()));
        return restBody;
    }

    @PostMapping("/decrypt")
    public RestBody test2(@Decrypt @RequestBody Map<String,String> map){
        return RestBody.ok(map);
    }

}
